package com.sjy.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sjy.common.constant.UserConstants;
import com.sjy.common.core.domain.entity.SysDept;
import com.sjy.common.utils.StringUtils;
import io.jsonwebtoken.lang.Assert;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sjy.common.annotation.Log;
import com.sjy.common.core.controller.BaseController;
import com.sjy.common.core.domain.AjaxResult;
import com.sjy.common.enums.BusinessType;
import com.sjy.system.domain.SysTree;
import com.sjy.system.service.ISysTreeService;
import com.sjy.common.utils.poi.ExcelUtil;
import com.sjy.common.core.page.TableDataInfo;

/**
 * 树形数据存储Controller
 * 
 * @author lzm
 * @date 2023-06-19
 */
@RestController
@RequestMapping("/system/tree")
public class SysTreeController extends BaseController
{
    @Autowired
    private ISysTreeService sysTreeService;

    /**
     * 查询树形数据存储列表
     */
    @PreAuthorize("@ss.hasPermi('system:tree:list')")
    @GetMapping("/list")
    public AjaxResult list(SysTree sysTree)
    {
        List<SysTree> list = sysTreeService.selectSysTreeList(sysTree);
        return success(list);
    }
    /**
     * 查询部门列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('system:tree:list')")
    @GetMapping("/list/exclude/{treeId}")
    public AjaxResult excludeChild(@PathVariable(value = "treeId", required = false) Long treeId)
    {
        List<SysTree> list = sysTreeService.selectSysTreeList(new SysTree());
        list.removeIf(d -> d.getTreeId().intValue() == treeId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), treeId + ""));
        return success(list);
    }
    /**
     * 导出树形数据存储列表
     */
    @PreAuthorize("@ss.hasPermi('system:tree:export')")
    @Log(title = "树形数据存储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTree sysTree)
    {
        List<SysTree> list = sysTreeService.selectSysTreeList(sysTree);
        ExcelUtil<SysTree> util = new ExcelUtil<SysTree>(SysTree.class);
        util.exportExcel(response, list, "树形数据存储数据");
    }

    /**
     * 获取树形数据存储详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tree:query')")
    @GetMapping(value = "/{treeId}")
    public AjaxResult getInfo(@PathVariable("treeId") Long treeId)
    {
        return success(sysTreeService.selectSysTreeByTreeId(treeId));
    }

    /**
     * 新增树形数据存储
     */
    @PreAuthorize("@ss.hasPermi('system:tree:add')")
    @Log(title = "树形数据存储", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTree sysTree)
    {
        Assert.isTrue(!(sysTree.getParentId()==null && StringUtils.isBlank(sysTree.getTreeType())),"新增顶级节点时，必须选择对应节点类型");
        Assert.isTrue(sysTreeService.checkTreeNameUnique(sysTree),"新增节点'" + sysTree.getTreeName() + "'失败，节点名称已存在");

        return toAjax(sysTreeService.insertSysTree(sysTree));
    }

    /**
     * 修改树形数据存储
     */
    @PreAuthorize("@ss.hasPermi('system:tree:edit')")
    @Log(title = "树形数据存储", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTree sysTree)
    {
        Long treeId = sysTree.getTreeId();
        Assert.isTrue(sysTreeService.checkTreeNameUnique(sysTree),"修改节点'" + sysTree.getTreeName() + "'失败，名称已存在");
        Assert.isTrue(!treeId.equals(sysTree.getParentId()),"修改节点'" + sysTree.getTreeName() + "'失败，上级节点不能是自己");
        Assert.isTrue(sysTreeService.selectNormalChildrenTreeById(treeId) ==0,"不能修改包含子节点的节点！");
        return toAjax(sysTreeService.updateSysTree(sysTree));
    }

    /**
     * 删除树形数据存储
     */
    @PreAuthorize("@ss.hasPermi('system:tree:remove')")
    @Log(title = "树形数据存储", businessType = BusinessType.DELETE)
	@DeleteMapping("/{treeId}")
    public AjaxResult remove(@PathVariable Long treeId)
    {
        return toAjax(sysTreeService.deleteSysTreeByTreeId(treeId));
    }
}
